<?php

include $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/database/DatabaseConnection.php';
/**
 * Description of ConnectionManager
 *
 * @author Reinowin7
 */
class ConnectionManager 
{
    private static $ConnectionManagerInstance = null;

    private $dbConnection = null;

    //intentionally private, so current class can be instantiated only inside this class itself.
    private function __construct()
    {
        
    }

    public static function getInstance() 
    {
        if (is_null(self::$ConnectionManagerInstance))
        {
            self::$ConnectionManagerInstance = new ConnectionManager();
        }
        return self::$ConnectionManagerInstance;
    }


    private function openConnection()
    {
        $this->dbConnection = DatabaseConnection::getConnection();

        if (is_null($this->dbConnection))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public function getConnection()
    {
        if (is_null($this->dbConnection)) 
        {
            if ($this->openConnection()) 
            {
                return $this->dbConnection;
            }
            else
            {
                return null;
            }
        }
        else
        {
             return $this->dbConnection;
        }
    }

    public function closeResultSet($resultSet)
    {
        if ($resultSet != null and $resultSet instanceof resource)
        {
            mysql_free_result($resultSet);
        }
    }
    
    public function closeConnection() 
    {
        if (isset($this->dbConnection))
        {
            mysql_close($this->dbConnection);
        }
    }
}

?>
